package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.MessageConst;
import com.itheima.common.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.service.ValidateCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/member")
public class MemberController {

    @Reference
    ValidateCodeService validateCodeService;

    @Reference
    MemberService memberService;

    @Reference
    JedisPool jedisPool;

    @RequestMapping("/login")
    public Result login(HttpServletResponse response,Map<String, String> map){
        String telephone = map.get("telephone");
        String validateCode = map.get("validateCode");
        boolean b = validateCodeService.checkValidateCode(telephone, RedisMessageConstant.SENDTYPE_LOGIN, validateCode);

        if (!b){
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }

         Member member = memberService.findByTelephone(telephone);
        if (member == null){
            member = new Member();
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            memberService.add(member);
        }

        return new Result(true,MessageConst.LOGIN_SUCCESS);
    }

}
